var param = new Object({
	init : function() {
		this.userType = ''
		this.beginDate = ''
		this.endDate = ''
		this.userValue = ''
	},
	setParam : function() {
		this.init()
		this.userType = $("#userType").val()
		this.beginDate = $("#beginDate").val()
		this.endDate = $("#endDate").val()
		this.userValue = $("#userValue").val()
	}
});
//报表全局变量
var attachChar;
var epsAttachChar;
var areaAttachChar;
var d;
var servicetypedatatable
var websitedatatable
var communitydatatable
var up_flow_count_char
var down_flow_count_char

$(function() {
	// getList();
	$('#table tbody').on('click', 'tr', function() {
		if ($(this).hasClass('selected')) {
			$(this).removeClass('selected');
		} else {
			datatable.$('tr.selected').removeClass('selected');
			$(this).addClass('selected');
		}
	});

	// 图表
	up_flow_count_char = echarts.init(document.getElementById('up_flow_count_'));
	down_flow_count_char = echarts.init(document.getElementById('down_flow_count_'));
	get_gauge_char(up_flow_count_char,0)
	get_gauge_char(down_flow_count_char,0)
	netTimePsLine = echarts.init(document.getElementById('netTimePsLine'));
	netQualityChar = echarts.init(document.getElementById('netQualityPsLine'));
	kqiPsLineChar = echarts.init(document.getElementById('kqiPsLine'));
//	epsAttachChar = echarts.init(document.getElementById('epsAttachChar'));
//	areaAttachChar = echarts.init(document.getElementById('areaAttachChar'));
	if(check_date($("#beginDate").val(),$("#endDate").val()) && !($("#msisdn").val()==null ||  $("#msisdn").val()=='')){
		getList();
	}
})
//初始化init
function get_gauge_char(charvar,data) {
	var option = {
		tooltip : {
			formatter : "{a} <br/>{b} : {c}"
		},
		series : [ {
			name : '业务指标',
			type : 'gauge',
			detail : {
				formatter : '{value}'
			},
			data : [ {
				value : data,
				name : ''
			} ],
			splitNumber : 5,
			axisLine : { // 坐标轴线
				lineStyle : { // 属性lineStyle控制线条样式
					color : [ [ 0.2, '#44B7D3' ], [ 0.8, '#6B6455' ],[ 1, '#E42B6D' ] ],
					width : 10
				}
			},
			axisTick : { // 坐标轴小标记
				splitNumber : 10, // 每份split细分多少段
				length : 15, // 属性length控制线长
				lineStyle : { // 属性lineStyle控制线条样式
					color : 'auto'
				}
			},
			splitLine : { // 分隔线
				length : 22, // 属性length控制线长
				lineStyle : { // 属性lineStyle（详见lineStyle）控制线条样式
					color : 'auto'
				}
			},
			pointer : {
				width : 5
			}
		} ]
	};
	charvar.clear();
	charvar.showLoading({
		text : '正在努力的读取数据中...'
	});
	charvar.setOption(option, true);
	charvar.hideLoading();
}

function getList() {
	if($("#beginDate").val() > $("#endDate").val()) {
		alert("开始时间 不能大于 结束时间")
		return
		
	} else if(!check_date($("#beginDate").val(),$("#endDate").val())){
		alert("开始时间 与 结束时间在24小时内")
		return
	}
	if($("#userValue").val()==null ||  $("#userValue").val()==''){
		alert("请输入用户号码")
		return
	}
	param.setParam()
	
	console.log(param)
    getUserRouteInfo();
	getUserInfo();
    getUserFlow()
	getServiceTypeList()
	getCommunityList()
	getWebSiteList()
	showPic();
}


function getUserRouteInfo() {
    window.frames['map'].layerLoad();
    $.ajax({
        type : "POST",
        url : "../../mapAnalysis/UserRoute",
        data : {
            userType :param.userType,
            userValue :param.userValue,
            beginDate :param.beginDate,
            endDate : param.endDate
        },
        dataType : "json",
        success : function(data) {
            if(!data["business"]){
                window.frames['map'].noMapdata();
            }else{

//                window.frames['map'].initData(data,{
//                    type: "user",
//                    data: "mme",
//                    lonb: "lng",
//                    latb: "lat",
//                    displayField: [{label:"开始时间",value:"start_date"},{label:"基站名称",value:"enodeb_name"},{label:"详单名称",value:"mme_name"}]
//
//                });

                //     displayField: [{label:"开始时间",value:"start_date"},{label:"基站名称",value:"enodeb_name"},{label:"详单名称",value:"mme_name"}],
                window.frames['map'].AddUserAnalysisData(data,{
                    type: "user",
                    data: "business",
                    lonb: "lng",
                    latb: "lat",
                    displayField: [{label:"开始时间",value:"start_date"},
                                   {label:"结束时间",value:"end_date"},
                                   {label:"基站名称",value:"enodeb_name"},
                                   {label:"TAU成功",value:"success_num"},
                                   {label:"TAU失败",value:"fail_num"},
                                   {label:"ATTACH成功",value:"success_num"},
                                   {label:"ATTACH失败",value:"fail_num"}
                                   ]
                },{
                    icon:window.frames['map'].Icons.blue
                });
                window.frames['map'].closeLayer();
            }


         }
    });
}

function getUserInfo() {
	$("#msisdn_").html($("#userValue").val());
	$.ajax({
		type : "POST",
		url : "../../excavantionAnalysis/useranalysis/getuserinfo",
		data : {
            userType :param.userType,
            userValue :param.userValue
		},
		dataType : "json",
		success : function(data) {
			console.log(data);
            if(!data["allData"][0] && typeof(data["allData"][0])!="undefined" && data["allData"][0]!=0){
                $("#user_terminal_").html(data["allData"][0].terminal_type);
                $("#regarea").html(data["allData"][0].area);
            }
        }


//			$("#user_terminal_").html(data["allData"][0].terminal_type)
//			$("#regarea").html(data["allData"][0].area)

	});
}

function getUserFlow() {
	$.ajax({
		type : "POST",
		url : "../../excavantionAnalysis/useranalysis/getuserflow",
		data : {
            userType :param.userType,
            userValue :param.userValue,
            beginDate :param.beginDate,
            endDate : param.endDate
		},
		dataType : "json",
		success : function(data) {
			console.log(data)
			if (!data["allData"][0] && typeof(data["allData"][0])!="undefined" && data["allData"][0]!=0)
			{
				$("#up_flow_count").html(data["allData"][0].up_flow_count)
				$("#down_flow_count").html(data["allData"][0].down_flow_count)
				get_gauge_char(up_flow_count_char,data["allData"][0].up_flow_count)
				get_gauge_char(down_flow_count_char,data["allData"][0].down_flow_count)
			}
//			$("#up_flow_count").html(data["allData"][0].up_flow_count)
//			$("#down_flow_count").html(data["allData"][0].down_flow_count)
//			get_gauge_char(up_flow_count_char,data["allData"][0].up_flow_count)
//			get_gauge_char(down_flow_count_char,data["allData"][0].down_flow_count)
		}
	});
}



function getWebSiteList() {

	$("#website tbody tr").remove();
	if (typeof (websitedatatable) == "undefined"
			|| websitedatatable == undefined || websitedatatable == "undefined"
			|| websitedatatable == null) {
	} else {
		websitedatatable.destroy();
	}
	websitedatatable = $('#website')
			.DataTable(
					{
						"aoColumnDefs" : [ {
							sDefaultContent : '',
							aTargets : [ '_all' ]
						} ],
						"bPaginate" : true, // 翻页功能
						"iDisplayLength" : 50,
						"bLengthChange" : false, // 改变每页显示数据数量
						"bFilter" : false, // 过滤功能
						"ordering": true, //排序功能
						"bSort" : true, // 排序功能
						"bInfo" : true,// 页脚信息
						"bAutoWidth" : true,// 自动宽度
						"iDisplayLength" : 50,
						// "sPaginationType": "full_numbers",//显示分页的样式
						// 还有simple_numbers等
//						"sScrollX" : "100%",
						"sScrollXInner" : "110%",
						"bScrollCollapse" : true,
						"oLanguage" : {
							"sLengthMenu" : "每页显示 _MENU_条",
							"sZeroRecords" : "没有找到符合条件的数据",
							"sProcessing" : "&nbsp;&nbsp;&nbsp;&nbsp;查询中，请稍等...",
							"sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
							"sInfoEmpty" : "没有记录",
							"sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
							"sSearch" : "搜索：",
							"oPaginate" : {
								"sFirst" : "首页",
								"sPrevious" : "前一页",
								"sNext" : "后一页",
								"sLast" : "尾页"
							}
						},
						"bProcessing" : true,
						"bServerSide" : true,
						"sAjaxSource" : "../../excavantionAnalysis/useranalysis/getwebsitelist",
						"fnServerData" : function(sSource, aoData, fnCallback) {
							$
									.ajax({
										"type" : "post",
										"contentType" : "application/x-www-form-urlencoded",
										"url" : sSource,
										"dataType" : "json",
										"data" : {
											"aoData" : JSON.stringify(aoData),
								            userType :param.userType,
								            userValue :param.userValue,
								            beginDate :param.beginDate,
								            endDate : param.endDate
										}, // 以json格式传递
										"success" : function(resp) {
											fnCallback(resp.pageDate);
											d = resp.allData;
										}
									});
						},
						columns : [  {
							data : "web_site"
						}, {
							data : "request_num"
						}, {
							data : "flow_count"
						}, {
							data : "up_flow_count"
						}, {
							data : "down_flow_count"
						}, {
							data : "up_packet"
						}, {
							data : "down_packet"
						} ]
					});
}

function getCommunityList() {
	$("#community tbody tr").remove();
	if (typeof (communitydatatable) == "undefined"
			|| communitydatatable == undefined
			|| communitydatatable == "undefined" || communitydatatable == null) {
	} else {
		communitydatatable.destroy();
	}
	communitydatatable = $('#community')
			.DataTable(
					{
						"aoColumnDefs" : [ {
							sDefaultContent : '',
							aTargets : [ '_all' ]
						} ],
						"bPaginate" : true, // 翻页功能
						"iDisplayLength" : 50,
						"bLengthChange" : false, // 改变每页显示数据数量
						"bFilter" : false, // 过滤功能
						"ordering": true, //排序功能
						"bSort" : true, // 排序功能
						"bInfo" : true,// 页脚信息
						"bAutoWidth" : true,// 自动宽度
						"iDisplayLength" : 50,
						// "sPaginationType": "full_numbers",//显示分页的样式
						// 还有simple_numbers等
//						"sScrollX" : "100%",
						"sScrollXInner" : "110%",
						"bScrollCollapse" : true,
						"oLanguage" : {
							"sLengthMenu" : "每页显示 _MENU_条",
							"sZeroRecords" : "没有找到符合条件的数据",
							"sProcessing" : "&nbsp;&nbsp;&nbsp;&nbsp;查询中，请稍等...",
							"sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
							"sInfoEmpty" : "没有记录",
							"sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
							"sSearch" : "搜索：",
							"oPaginate" : {
								"sFirst" : "首页",
								"sPrevious" : "前一页",
								"sNext" : "后一页",
								"sLast" : "尾页"
							}
						},
						"bProcessing" : true,
						"bServerSide" : true,
						"sAjaxSource" : "../../excavantionAnalysis/useranalysis/getcommunitylist",
						"fnServerData" : function(sSource, aoData, fnCallback) {
							$
									.ajax({
										"type" : "post",
										"contentType" : "application/x-www-form-urlencoded",
										"url" : sSource,
										"dataType" : "json",
										"data" : {
											"aoData" : JSON.stringify(aoData),
								            userType :param.userType,
								            userValue :param.userValue,
								            beginDate :param.beginDate,
								            endDate : param.endDate
										}, // 以json格式传递
										"success" : function(resp) {
											fnCallback(resp.pageDate);
											d = resp.allData;
										}
									});
						},
						columns : [  {
							data : "cell_name"
						}, {
							data : "cell_id"
						}, {
							data : "flow_count"
						}, {
							data : "frist_pack_time_delay"
						}, {
							data : "speed"
						} ]
					});
}

function getServiceTypeList() {
	$("#service_type tbody tr").remove();
	if (typeof (servicetypedatatable) == "undefined"
			|| servicetypedatatable == undefined
			|| servicetypedatatable == "undefined"
			|| servicetypedatatable == null) {
	} else {
		servicetypedatatable.destroy();
	}
	servicetypedatatable = $('#service_type')
			.DataTable(
					{
						"aoColumnDefs" : [ {
							sDefaultContent : '',
							aTargets : [ '_all' ]
						} ],
						"bPaginate" : true, // 翻页功能
						"iDisplayLength" : 50,
						"bLengthChange" : false, // 改变每页显示数据数量
						"bFilter" : false, // 过滤功能
						"ordering": true, //排序功能
						"bSort" : true, // 排序功能
						"bInfo" : true,// 页脚信息
						"bAutoWidth" : true,// 自动宽度
						"iDisplayLength" : 50,
						// "sPaginationType": "full_numbers",//显示分页的样式
						// 还有simple_numbers等
//						"sScrollX" : "100%",
						"sScrollXInner" : "110%",
						"bScrollCollapse" : true,
						"oLanguage" : {
							"sLengthMenu" : "每页显示 _MENU_条",
							"sZeroRecords" : "没有找到符合条件的数据",
							"sProcessing" : "&nbsp;&nbsp;&nbsp;&nbsp;查询中，请稍等...",
							"sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
							"sInfoEmpty" : "没有记录",
							"sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
							"sSearch" : "搜索：",
							"oPaginate" : {
								"sFirst" : "首页",
								"sPrevious" : "前一页",
								"sNext" : "后一页",
								"sLast" : "尾页"
							}
						},
						"bProcessing" : true,
						"bServerSide" : true,
						"sAjaxSource" : "../../excavantionAnalysis/useranalysis/getservicetypelist",
						"fnServerData" : function(sSource, aoData, fnCallback) {
							$
									.ajax({
										"type" : "post",
										"contentType" : "application/x-www-form-urlencoded",
										"url" : sSource,
										"dataType" : "json",
										"data" : {
											"aoData" : JSON.stringify(aoData),
								            userType :param.userType,
								            userValue :param.userValue,
								            beginDate :param.beginDate,
								            endDate : param.endDate
										}, // 以json格式传递
										"success" : function(resp) {
											fnCallback(resp.pageDate);
											d = resp.allData;
										}
									});
						},
						columns : [ {
							data : "business_group"
						}, {
							data : "octets"
						}, {
							data : "frist_pack_time_delay"
						}, {
							data : "speed"
						} ]
					});
}

function showPic() {
	$("#pic").show();
	initNetTimeChar();
	initnetQualityPsLineChar();
	initkqiChar();
}
function initkqiChar() {
	// 查询
	kqiPsLineChar.clear();
	kqiPsLineChar.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../excavantionAnalysis/useranalysis/getkqichar?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
            userType :param.userType,
            userValue :param.userValue,
            beginDate :param.beginDate,
            endDate : param.endDate
		},
		success : function(data) {
			kqiPsLineChar.setOption(data, true);
			kqiPsLineChar.hideLoading();
		}
	});
}


function initNetTimeChar() {
	// 查询
	netTimePsLine.clear();
	netTimePsLine.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../excavantionAnalysis/useranalysis/getnettimechar?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
            userType :param.userType,
            userValue :param.userValue,
            beginDate :param.beginDate,
            endDate : param.endDate
		},
		success : function(data) {
			netTimePsLine.setOption(data, true);
			netTimePsLine.hideLoading();
		}
	});
}

function initnetQualityPsLineChar() {
	// 查询
	netQualityChar.clear();
	netQualityChar.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../excavantionAnalysis/useranalysis/getnetqualitychar?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
            userType :param.userType,
            userValue :param.userValue,
            beginDate :param.beginDate,
            endDate : param.endDate
		},
		success : function(data) {
			netQualityChar.setOption(data, true);
			netQualityChar.hideLoading();
		}
	});
}

function resetQueryForm() {
	$('#queryForm')[0].reset();
}

function linkToURLUserDetails() {
	var beginDate = $("#beginDate").val();
	var endDate = $("#endDate").val();
	var userValue = $("#userValue").val();
	var userType = $("#userType").val();
	if(beginDate > endDate) {
		alert("开始时间 不能大于 结束时间")
		return
		
	} else  if(!check_date(beginDate,endDate)){
		alert("开始时间与结束时间在24小时内")
		return
	}
	if(userValue==null ||  userValue==''){
		alert("请输入用户号码")
		return
	}
	linkTab("用户详单","/kpi/userOnlineDetails/index?beginDate=" + beginDate  + "&endDate=" + endDate +"&userType=" + userType + "&userValue=" + userValue)
}